import { Entity, Column } from 'typeorm';
import { BasePo } from '@/common/base.po';

@Entity({
  name: 'order',
  comment: '订单',
})
export class Order extends BasePo {
  constructor(partial: any) {
    super();
    Object.assign(this, partial);
    this.status = OrderStatus.UNPAID;

  }

  @Column({
    name: 'user_id',
    type: 'varchar',
    length: 32,
    comment: '用户Id',
  })
  userId: string;

  @Column({
    type: 'varchar',
    length: 20,
    comment: '订单状态',
    nullable: true,
  })
  status: OrderStatus;

  @Column({
    type: 'decimal',
    precision: 8,
    scale: 2,
    comment: '订单价格',
    nullable: true,
    transformer: {
      to: (value: number) => value,
      from: (value: string) => parseFloat(value),
    },
  })
  price: number;

  @Column({
    name: 'prepay_id',
    type: 'varchar',
    length: 50,
    comment: '微信预支付订单id',
    nullable: true,
  })
  prepayId?: string;

  @Column({
    name: 'refund_id',
    type: 'varchar',
    length: 50,
    comment: '微信退款订单id',
    nullable: true,
  })
  refundId?: string;

  @Column({
    name: 'pay_time',
    type: 'datetime',
    comment: '支付时间',
    nullable: true,
  })
  payTime?: Date;

  @Column({
    name: 'refund_time',
    type: 'datetime',
    comment: '退款时间',
    nullable: true,
  })
  refundTime?: Date;

  @Column({
    type: 'varchar',
    length: 200,
    comment: '订单备注',
    nullable: true,
  })
  remarks?: string;

  // - time 预约时间
  // - desc 备注
  // - image_list 图片列表（取件码截图）
  // - phone 手机号
  // - taker_userid 接单人用户id
  // - take_num 快递个数
  // - take_big_num 大件个数
  // - order_type 订单类型(取快递，扔垃圾)
  // - status 订单状态（创建中，创建成功，支付成功，接单成功，订单完成，已退款）
  // - order_amount订单金额
  // - coupon_id 优惠券id 

  @Column({
    name: 'time',
    type: 'varchar',
    length: 20,
    comment: '预约时间',
    nullable: true,
  })
  time?: string;

  @Column({
    name: 'time_end',
    type: 'varchar',
    length: 20,
    comment: '预约结束时间',
    nullable: true,
  })
  timeEnd?: string;

  @Column({
    type: 'varchar',
    length: 200,
    comment: '备注',
    nullable: true,
  })
  desc?: string;

  @Column({
    name: 'image_list',
    type: 'json',
    comment: '图片列表',
    nullable: true,
  })
  imageList?: string[];

  @Column({
    name: 'phone',
    type: 'varchar',
    length: 20,
    comment: '手机号',
    nullable: true,
  })
  phone?: string;

  @Column({
    name: 'taker_userid',
    type: 'varchar',
    length: 32,
    comment: '接单人用户id',
    nullable: true,
  })
  takerUserId?: string;

  @Column({
    name: 'taker_finish_time',
    type: 'datetime',
    comment: '接单人完成时间',
    nullable: true,
  })
  takerFinishTime?: Date;

  @Column({
    name: 'take_num',
    type: 'int',
    comment: '快递个数',
    nullable: true,
  })
  takeNum?: number;

  @Column({
    name: 'take_big_num',
    type: 'int',
    comment: '大件个数',
    nullable: true,
  })
  takeBigNum?: number;

  @Column({
    name: 'order_type',
    type: 'varchar',
    length: 20,
    comment: '订单类型',
    nullable: true,
  })
  orderType?: string;

  @Column({
    name: 'order_amount',
    type: 'decimal',
    precision: 8,
    scale: 2,
    comment: '订单金额',
    nullable: true,
  })
  orderAmount?: number;

  @Column({
    name: 'coupon_id',
    type: 'varchar',
    length: 32,
    comment: '优惠券id',
    nullable: true,
  })
  couponId?: string;

  @Column({
    name: 'full_address',
    type: 'varchar',
    length: 200,
    comment: '完整地址',
    nullable: true,
  })
  fullAddress?: string;
}

export enum OrderStatus {
  UNPAID = 'unpaid',
  PAID = 'paid',
  FINISHED = 'finished',
  CANCELED = 'canceled',
  REFUNDING = 'refunding',
  REFUNDED = 'refunded',
  EXCEPTION = 'exception',
}
